package com.showsu.editor.controller.biz;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.showsu.editor.controller.BaseController;
import com.showsu.editor.entity.doo.VipGoods;
import com.showsu.editor.entity.vo.response.VipInfoVo;
import com.showsu.editor.entity.vo.response.VipPrivilegeInfoVo;
import com.showsu.editor.entity.vo.response.VipPrivilegeTypeVo;
import com.showsu.editor.entity.vo.response.VipTypeVo;
import com.showsu.editor.enums.VipGoodsEnum;
import com.showsu.editor.service.IVipGoodsService;
import com.showsu.editor.utils.BsResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 7:54 下午 2024/9/13
 * @Modified By:
 */
@RestController
@Slf4j
@Api(value = "CommonController", tags = "通用信息")
public class CommonController extends BaseController {

    @Autowired
    private IVipGoodsService vipGoodsService;

    @PostMapping("/api/vip/info/list")
    @ApiOperation(value = "会员信息列表", notes = "会员信息列表")
    @ResponseBody
    public BsResult vipInfoList(){
        List<VipGoods> vipGoodsList = vipGoodsService.queryAll();
        //创建返回对象，把数据塞进去
        List<VipTypeVo> resultLis = new ArrayList<>();
        for (VipGoodsEnum vipGoodsEnum : VipGoodsEnum.values()) {
            if (vipGoodsEnum.equals(VipGoodsEnum.FreeEditioin)) {
                continue;
            }
            VipTypeVo vipTypeVo = new VipTypeVo();
            resultLis.add(vipTypeVo);
            vipTypeVo.setVipVersion(vipGoodsEnum.getVipVersion());
            vipTypeVo.setVipVersionDesc(vipGoodsEnum.getVipVersionDesc());
        }

        for (VipTypeVo vipTypeVo : resultLis) {
            List<VipInfoVo> vipList = new ArrayList<>();
            vipTypeVo.setVipList(vipList);
            for (VipGoods vipGoods : vipGoodsList) {
                if (! vipGoods.getVipVersion().equals(vipTypeVo.getVipVersion())) {
                   continue;
                }
                VipInfoVo vipInfoVo = new VipInfoVo();
                vipList.add(vipInfoVo);

                vipInfoVo.setVipLevel(vipGoods.getVipLevel());
                vipInfoVo.setVipName(vipGoods.getVipName());
                vipInfoVo.setPrice(vipGoods.getPrice());
                vipInfoVo.setIsDefault(vipGoods.getIsDefault());
                //设置套餐对应的权限列表
                if (StringUtils.isBlank(vipGoods.getPrivilegeInfo())) {
                    continue;
                }
                try {
                    Type type = new TypeToken<List<VipPrivilegeTypeVo>>(){}.getType();
                    Gson gson = new Gson();
                    List<VipPrivilegeTypeVo> vipPrivilegeTypeVos = gson.fromJson(vipGoods.getPrivilegeInfo(), type);
                    vipInfoVo.setPrivilegeTypeList(vipPrivilegeTypeVos);
                } catch (Exception e) {
                    log.error("CommonController.vipInfoList() error", e);
                }
            }
        }
        return BsResult.success(resultLis);
    }
}
